<?php

Solar::loadClass('Solar_View_Helper');

class Domain51_View_Helper_Css extends Solar_View_Helper
{
    static protected $_css_files = array();
    static protected $_inline_css = array();
    
    public function __construct($config = null) {
        parent::__construct($config);
    }
    
    public function css() {
        return $this;
    }
    
    /**
     * Adds a CSS file to be display when {@link render()} is called.
     *
     * This requires that the CSS file be known to the {@link Domain51_App_Css}.
     * If you want to add a raw file see {@link addRawCssFile()}.
     *
     * @param   string  $name   Name of the CSS file to load
     * @see     addRawCssFile(), $known_css_files
     *
     * @todo Remove hardcoded reference to path/uri
     */
    public function addCssFile($name) {
        $public_location = '/Solar/index.php/';
        $href =  $public_location . 'css/' . $name;
        $this->addRawCssFile($href);
    }
    
    
    /**
     * Adds a raw link to a CSS file to be displayed at {@link render()}
     *
     * If the CSS file you want to access is configured to be used with
     * {@link Domain51_App_Css}, you should use {@link addCssFile()}
     *
     * @param   string  $href   URL of a CSS file.
     * @see     addCssFile()
     */
    public function addRawCssFile($href) {
        self::$_css_files[] = $href;
    }
    
    
    /**
     * Adds inline CSS to be displayed at {@link render()}
     *
     * @param   string  $string
     */
    public function addInlineCss($string) {
        self::$_inline_css = $string;
    }
    
    
    /**
     * Returns the rendered the CSS that's be entered via {@link addCssFile()},
     * {@link addRawCssFile()}, and {@link addInlineCss()}.
     *
     *
     * @return  string
     */
    public function render() {
        $return = '';
        
        foreach (self::$_css_files as $css_href) {
            $return .= "<link rel='stylesheet' href='{$css_href}' />\r\n";
        }
        
        if (count(self::$_inline_css) > 0) {
            $return .= "<style type='text/css'>\r\n" 
                . implode("\r\n", self::$_inline_css) . "\r\n"
                . "</style>";
        }
        
        return $return;
    }
}

?>
